namespace eval phook {}

proc phook::add {from command} {
variable fr
set fr($from) $command
}
 
proc phook::remove {from} {
    variable fr
	unset fr($from)
}


proc phook::run {from type body} {
	if {$type=="chat"} {
		variable fr
		if {![info exists fr($from)]} {
			return
		} else {
			set code [catch { eval $fr($from) [list $from $body]} state]
			if {$code == 1} {
				putlog  [format "Comand %s failed\nProcedure %s returned code\
				       %s\n\n%s" $fr($from) [list $from $body] $code $state]
			}
			if {(!$code) && ([cequal $state stop])} {
				return stop
			}
		}
	}
}
hook::add message phook::run 29